#-*- coding: Utf-8 -*-  
from django.template.loader import render_to_string
from django.utils.safestring import mark_safe
from django import forms
from django.conf import settings
import os

tpldir = os.path.realpath(os.path.dirname(__file__)) + '/templates/'
if not tpldir in settings.TEMPLATE_DIRS:
    settings.TEMPLATE_DIRS += (tpldir,)    

class AdminFileWidget(forms.FileInput):
    def render(self, name, value, attrs=None):
        output = []
        if value and hasattr(value, 'url'):            
            output.append(render_to_string('file_widget.html', {'value': value }))
        output.append(super(self.__class__, self).render(name, value, attrs))
        return mark_safe(u''.join(output))

class AdminImageWidget(forms.FileInput):            
    def render(self, name, value, attrs=None):
        output = []
        if value and hasattr(value, 'url'):            
            output.append(render_to_string('image_widget.html', {'value': value }))
        output.append(super(self.__class__, self).render(name, value, attrs))
        return mark_safe(u''.join(output))

class VisualEditorWidget(forms.Textarea):
    def __init__(self, attrs=None):
        if not attrs: attrs = {}
        attrs['class'] = 'visual_editor'
        return super(VisualEditorWidget, self).__init__(attrs)
                    
    class Media:
        if hasattr(settings, 'VISUAL_EDITOR_JS'):
            js = settings.VISUAL_EDITOR_JS
        if hasattr(settings, 'VISUAL_EDITOR_CSS'):
            css = {'all': settings.VISUAL_EDITOR_CSS,}

#class AdminGooglemapsWidget(forms.HiddenInput):    
#    def render(self, name, value, attrs=None):
#        if value is None: value = ''
#        # TODO
#        final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
##        if value != '':
##            # Only add the 'value' attribute if a value is non-empty.
##            final_attrs['value'] = force_unicode(value)
#        #return mark_safe(u'<input%s />' % flatatt(final_attrs))
#        return mark_safe(u''.join(output))
#    
#    class Media:
#        js = ('http://maps.google.com/maps?file=api&amp;v=2&amp;key=%s' % settings.GOOGLEMAPS_KEY,)
    